package com.android.unipluginpicturewater;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Description: UniPlugin-Hello-AS
 * Author:     yangtong<br>
 * Date:    2023/5/15    15:51
 * Version:
 * Update:
 */
public class CompressUtils {
    /**
     * 图片压缩    大于1m进行有损压缩，时1m倍数先缩放在进行质量压缩
     *
     * @param file
     * @return
     */

    public static File compressImage(File file, int numK) {
        long fileM = file.length() / 1024;
        if (fileM < numK)
            return file;
        int radio = (int) (fileM / numK);
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        Bitmap result;
        if (radio > 1) {
            result = Bitmap.createBitmap(bitmap.getWidth() / radio, bitmap.getHeight() / radio, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(result);
            RectF rectF = new RectF(0, 0, bitmap.getWidth() / radio, bitmap.getHeight() / radio);
            //将原图画在缩放之后的矩形上
            canvas.drawBitmap(bitmap, null, rectF, null);
        } else {
            result = bitmap;
        }

        int options = 90;
        result.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        do {
            options -= 10;
            bos.reset();
            result.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        while (bos.toByteArray().length / 1024 >= numK && options >= 0);


//        LogUtils.debug("结束" + bos.toByteArray().length / 1024 + "K");
        if (bos.toByteArray().length / 1024 < numK) {
            try {
                File sdFile = Environment.getExternalStorageDirectory();
                File fileNew = new File(sdFile, System.currentTimeMillis() + ".jpg");
                FileOutputStream fos = new FileOutputStream(fileNew);
                fos.write(bos.toByteArray());
                fos.flush();
                fos.close();
                return fileNew;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static File compressImage(Bitmap bitmap, float numK) {
        int options = 100;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);

        float radio =  bos.toByteArray().length / 1024 / numK ;
        Bitmap result;
        if (radio > 1) {
            result = Bitmap.createBitmap((int) (bitmap.getWidth() / radio), (int) (bitmap.getHeight() / radio), Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(result);
            RectF rectF = new RectF(0, 0, bitmap.getWidth() / radio, bitmap.getHeight() / radio);
            //将原图画在缩放之后的矩形上
            canvas.drawBitmap(bitmap, null, rectF, null);
        } else {
            result = bitmap;
        }

        do {
            options -= 10;
            bos.reset();
            result.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        while (bos.toByteArray().length / 1024 >= numK && options >= 0);

//        LogUtils.debug("结束" + bos.toByteArray().length / 1024 + "K");
        if (bos.toByteArray().length / 1024 < numK) {
            try {
                File sdFile = Environment.getExternalStorageDirectory();
                File fileNew = new File(sdFile, System.currentTimeMillis() + ".jpg");
                FileOutputStream fos = new FileOutputStream(fileNew);
                fos.write(bos.toByteArray());
                fos.flush();
                fos.close();
                return fileNew;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 无图片后缀的图片
     *
     * @param bitmap
     * @param numK
     * @return
     */
    public static File compressImageNoPostfix(Bitmap bitmap, int numK) {
        int options = 100;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);

        int radio = bos.toByteArray().length / 1024 / numK;
        Bitmap result;
        if (radio > 1) {
            result = Bitmap.createBitmap(bitmap.getWidth() / radio, bitmap.getHeight() / radio, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(result);
            RectF rectF = new RectF(0, 0, bitmap.getWidth() / radio, bitmap.getHeight() / radio);
            //将原图画在缩放之后的矩形上
            canvas.drawBitmap(bitmap, null, rectF, null);
        } else {
            result = bitmap;
        }

        do {
            options -= 10;
            bos.reset();
            result.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        while (bos.toByteArray().length / 1024 >= numK && options >= 0);

//        Log.d("结束" + bos.toByteArray().length / 1024 + "K");
        if (bos.toByteArray().length / 1024 < numK) {
            try {
                File sdFile = Environment.getExternalStorageDirectory();
                File fileNew = new File(sdFile, System.currentTimeMillis() + "");
                FileOutputStream fos = new FileOutputStream(fileNew);
                fos.write(bos.toByteArray());
                fos.flush();
                fos.close();
                return fileNew;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }



    public static File compressImage(File file) {
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
        try {
            File sdFile = Environment.getExternalStorageDirectory();
            File fileNew = new File(sdFile, System.currentTimeMillis() + ".jpg");
            FileOutputStream fos = new FileOutputStream(fileNew);
            fos.write(bos.toByteArray());
            fos.flush();
            fos.close();
            return fileNew;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




    public static long getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
            return bitmap.getByteCount();
        }
        // 在低版本中用一行的字节x高度
        return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
    }
}
